package com.upcode.weighing.oauthserver.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.upcode.weighing.domain.SysOptLog;
import com.upcode.weighing.oauthserver.mapper.SysOptLogRecMapper;
import com.upcode.weighing.util.ClientRequestInfoUtil;
import com.upcode.weighing.vo.req.PageReqVo;
import com.upcode.weighing.vo.req.pc.SysLoginLogListVo;
import com.upcode.weighing.vo.resp.pc.SysLogRespVo;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 日志信息表 服务实现类
 * </p>
 *
 * @author alex
 * @since 2019-04-10
 */
@Service
public class SysLogRecServiceImpl extends ServiceImpl<SysOptLogRecMapper, SysOptLog> implements SysOptLogRecService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysLogRecServiceImpl.class);

    @Autowired
    SysOptLogRecMapper sysOptLogMapper;

    @Override
    @Async
    public void addSysLoginLog(String loginName, HttpServletRequest request) {
        //异步的Feign调用需要重新初始化HystrixRequestContext
        HystrixRequestContext.initializeContext();
        SysOptLog sysLog = new SysOptLog();
        LocalDateTime date = LocalDateTime.now();
        String ip = ClientRequestInfoUtil.getClientIp(request);
        String browser = ClientRequestInfoUtil.getBrowser(request);
        String area = "";
//        if(!ip.isEmpty()){
//            try{
//                area = ClientRequestInfoUtil.getIpInfo(ip);
//            }catch (Exception e){
//                LOGGER.error("阿里获取IP地址异常！"+e.getMessage(),e);
//            }
//        }
        sysLog.setId(IdWorker.getId());
        sysLog.setCreateUser(loginName);
        sysLog.setAdr(ip);
        sysLog.setDesc(browser);
        sysLog.setCreateTime(date);
        sysLog.setStatus(1);
        sysLog.setRemark(area);
        sysLog.setType(5);
        sysOptLogMapper.addLoginRec(sysLog);
        LOGGER.info(loginName+"登录日志添加成功");
    }
}
